from pyecharts.charts import Map
from pyecharts.options import *
import json

#------------------------test_function--------------------------
def my_function():
    map = Map()
    #数据都是列表形式

    data = [
        ("北京市", 99),
        ("上海市", 199),
        ("重庆市", 89),
        ("江苏省南京市", 97),
        ("江苏省苏州市", 93)
    ]
    #添加数据
    map.add("测试地图", data, "china")

    #设置全局选项
    map.set_global_opts(
        visualmap_opts=VisualMapOpts(
            is_show=True,
            is_piecewise=True,
            pieces=[
                {"min": 1, "max": 9, "label": "1-9", "color": "#CCFFFF"},
                {"min": 10, "max": 99, "label": "10-99", "color": "#FF666"},
                {"min": 100, "max": 500, "label": "100-500", "color": "#990033"}
            ]
        )

    )

    #绘图
    map.render()

#------------------------test_function END--------------------------
f = open("d:\语法\资料\可视化案例数据\地图数据\疫情.txt", "r", encoding="UTF-8")
data = f.read()
f.close()

#将json文件转化为字典
data_dic = json.loads(data)
#从字典里面取出省份数据
province_data_list = data_dic["areaTree"][0]["children"]

#组装省份和确诊人数为元组，并将其封装到列表中
data_list = []
for province_data in province_data_list:#这儿地province_data相当于areaTree中的每个字典
    #在后面追加市或者省
    if province_data["name"] in ["北京", "天津", "上海", "重庆"]:
        province_name = province_data["name"] + "市"
    else:
        province_name = province_data["name"] + "省"

    province_confirm = province_data["total"]["confirm"]    #省份确诊人数
    data_list.append((province_name, province_confirm))

# print(data_list)

#创建地图
map = Map()
map.add("各省份确诊人数", data_list, "china")

#设置全局选项
map.set_global_opts(
    title_opts=TitleOpts(title = "全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min":1, "max":99, "label":"1-99人", "color":"#CCFFFF"},
            {"min":100, "max":999, "label":"100-999人", "color":"#FFFF99"},
            {"min":1000, "max":4999, "label":"1000-4999人", "color":"#FF9966"},
            {"min":5000, "max":9999, "label":"5000-9999人", "color":"#FF6666"},
            {"min":10000, "max":99999, "label":"10000-99999人", "color":"#CC3333"},
            {"min":100000, "label":"100000+", "color":"#990033"},
        ]
    )
)

#生成
map.render("全国疫情地图.html")
